package baiqi.sishen.community.controller;

import baiqi.sishen.community.cache.HotTagCache;
import baiqi.sishen.community.dto.QuestionDTO;
import baiqi.sishen.community.schedule.HotTagTasks;
import baiqi.sishen.community.service.QuestionService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/4/26
 */
@Controller
public class IndexController {

    @Resource
    private QuestionService questionService;

    @Resource
    private HotTagCache hotTagCache;

    @GetMapping("/")
    public String index(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "5") Integer size,
            @RequestParam(value = "search", required = false) String search,
            @RequestParam(value = "tag", required = false) String tag,
            @RequestParam(value = "sort", required = false) String sort,
            Model model) {

        //查询首页问题数据
        PageInfo<QuestionDTO> questions = questionService.list(sort, tag, search, page, size);
        //查询首页置顶问题数据
        List<QuestionDTO> topQuestions = questionService.listTop(sort, tag, search);
        List<String> tags = hotTagCache.getHots();//获取热门话题的标签
        model.addAttribute("pagination", questions);
        model.addAttribute("topQuestions", topQuestions);
        model.addAttribute("search", search);
        model.addAttribute("tags", tags);
        model.addAttribute("tag", tag);
        model.addAttribute("sort", sort);
        return "index";
    }
}
